#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

class Computer
{
public:
    Computer(const char * brand,double price)
    : _brand(new char[strlen(brand) + 1]())
    , _price(price)
    {
        cout << "Computer(const char *,double)" << endl;
        strcpy(_brand,brand);
    }
 
    void print() 
    {
        cout << "brand:" << _brand << endl;
        cout << "price:" << _price << endl;
    }

    ~Computer(){
        cout << "~Computer()" << endl;
        /* cout << _price << endl; */
        if(_brand){
            delete [] _brand;
            _brand = nullptr;//安全回收
        }
    }
private:
    char * _brand;
    double _price;
};



void test0(){
    Computer pc("huawei",7000);
    pc.print();
    /* cout << sizeof(Computer) << endl; */
    //不要手动调用析构函数
    //让对象在销毁时自动调用析构函数
    /* pc.~Computer(); */
    /* pc.print(); */
}

Computer pc1("apple",20000);

void test2(){
    Computer pc2("asus",5000);
    static Computer pc3("lenovo",7000);
    /* int * pint = new int(10); */
    Computer * p = new Computer("xiaomi",6000);
    delete p;

    cout << "&pc2:" << &pc2 << endl;
    cout << "&pc3:" << &pc3 << endl;
    cout << "&pc1:" << &pc1 << endl;
}

void test1(){
    int * p1 = nullptr;
    cout << p1 << endl;

    //<<对char型指针有默认重载效果
    //会访问指针指向地址的内容
    //此处对空指针指向地址进行访问，导致程序中断
    char * p = nullptr;
    cout << p << endl;
}

int main(void){
    test2();
    cout << "over" << endl;
    return 0;
}
